﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace Quan_Ly_Phong_Kham_co_len_em
{
    public partial class frmXquang_CT : Form
    {
        public static string _tenloai;
        public static string _maloai;
        public static DataTable _dtXQ = new DataTable();
        public static long _sum = 0;

        public frmXquang_CT()
        {
            InitializeComponent();
            DataGridViewTextBoxColumn col = new DataGridViewTextBoxColumn();

            TV_Tonghop.Themcot("MA_LOAI_DVCLS", "Mã Dịch Vụ", dt_Xquang_CT, DataGridViewAutoSizeColumnMode.Fill);
            TV_Tonghop.Themcot("TEN_LOAI_DVCLS", "Tên Dịch Vụ ", dt_Xquang_CT, DataGridViewAutoSizeColumnMode.Fill);
            TV_Tonghop.Themcot("NGAY_THANG_NAM", "Ngày Áp Dụng", dt_Xquang_CT, DataGridViewAutoSizeColumnMode.Fill);
            TV_Tonghop.Themcot("DON_GIA_DV_CLS", "Đơn Giá", dt_Xquang_CT, DataGridViewAutoSizeColumnMode.Fill);
        }

        //ham ket noi co so du lieu
        public SqlConnection getconnect()
        {
            return new SqlConnection("Data Source=NGUYENTY-PC\\SQLEXPRESS;Initial Catalog=QL_PHONGKHAM;Integrated Security=True");

        }

        private void frmXquang_CT_Load(object sender, EventArgs e)
        {
            DataTable dtb = AddID.Ham_Tu_Tang.Select_Max("LOAI_DICH_VU_CLS", "Ma_Loai_Dvcls");
            string chuoi = dtb.Rows[0]["id"] + "";
            string kq = AddID.Ham_Tu_Tang.AddID(chuoi, 2, 2);

            txb_madichvu.Text = kq;

            SqlConnection ketnoi = getconnect();
            SqlDataAdapter ad = new SqlDataAdapter("HienThiDichVuCLS", ketnoi); //ket noi den co so du lieu de goi ham da khai bao trong StoredProcedure
            DataTable dt = new DataTable();
            ad.Fill(dt);
            dt_Xquang_CT.DataSource = dt;


            //SqlConnection ketnoi = getconnect();
            //SqlDataAdapter ad = new SqlDataAdapter("HienThiDichVuCLS", ketnoi);
            //DataTable dt1 = new DataTable();
            //ad.Fill(dt1);
            //dt_Xquang_CT.DataSource = dt1;

            //SqlConnection ketnoi = new SqlConnection("Data Source=NGUYENTY-PC\\SQLEXPRESS;Initial Catalog=QL_PHONGKHAM;Integrated Security=True");
            //ketnoi.Open();
            //SqlCommand cmd1 = new SqlCommand("Select * from Loai_Dich_Vu_CLS", ketnoi);
            //SqlCommand cmd2 = new SqlCommand("Select count(Ma_Loai_DVCLS) as dem from Loai_Dich_Vu_CLS", ketnoi);
            //DataTable dt2 = new DataTable();
            //DataTable dt3 = new DataTable();
            //dt2.Load(cmd1.ExecuteReader());
            //dt3.Load(cmd2.ExecuteReader());

        }

        private void bt_them_Click(object sender, EventArgs e)
        {
            SqlConnection ketnoi = getconnect();
            ketnoi.Open();
            SqlCommand cmd = new SqlCommand("ThemDichVuCLS", ketnoi);
            cmd.CommandType = CommandType.StoredProcedure;// kieu cau truy van SQL la kieu StoredProcedure
            cmd.Parameters.Add("@ma_loai_dvcls", SqlDbType.NChar).Value = txb_madichvu.Text;
            cmd.Parameters.Add("@ten_loai_dvcls", SqlDbType.NVarChar).Value = txb_tendichvu.Text;
            cmd.Parameters.Add("@ngay_thang_nam", SqlDbType.DateTime).Value = DateTime.Now;
            cmd.Parameters.Add("@don_gia_dv_cls", SqlDbType.BigInt).Value = Convert.ToInt64(txb_dongia.Text);
          


            cmd.ExecuteNonQuery();
            frmXquang_CT_Load(sender, e);

        }

        private void bt_sua_Click(object sender, EventArgs e)
        {
            SqlConnection ketnoi = getconnect();
            ketnoi.Open();
            SqlCommand cmd = new SqlCommand("CapNhatDichVuCLS", ketnoi);
            cmd.CommandType = CommandType.StoredProcedure;// kieu cau truy van SQL la kieu StoredProcedure
            cmd.Parameters.Add("@ma_loai_dvcls", SqlDbType.NChar).Value = txb_madichvu.Text;
            cmd.Parameters.Add("@ten_loai_dvcls", SqlDbType.NVarChar).Value = txb_tendichvu.Text;
            cmd.Parameters.Add("@ngay_thang_nam", SqlDbType.DateTime).Value = DateTime.Now;
            cmd.Parameters.Add("@don_gia_dv_cls", SqlDbType.BigInt).Value = Convert.ToInt64(txb_dongia.Text);



            cmd.ExecuteNonQuery();
            frmXquang_CT_Load(sender, e);
        }

        private void bt_xoa_Click(object sender, EventArgs e)
        {
            SqlConnection ketnoi = getconnect();
            ketnoi.Open();
            SqlCommand cmd = new SqlCommand("XoaDichVuCLS", ketnoi);
            cmd.CommandType = CommandType.StoredProcedure;// kieu cau truy van SQL la kieu StoredProcedure
            cmd.Parameters.Add("@ma_loai_dvcls", SqlDbType.NChar).Value = txb_madichvu.Text;


            cmd.ExecuteNonQuery();
            frmXquang_CT_Load(sender, e);
        }

        int dong;

        private void dt_Xquang_CT_RowEnter(object sender, DataGridViewCellEventArgs e)
        {
            dong = e.RowIndex;
                txb_madichvu.Text = dt_Xquang_CT.Rows[dong].Cells["MaDV"].Value.ToString();
            txb_tendichvu.Text = dt_Xquang_CT.Rows[dong].Cells["TenDV"].Value.ToString();
            dt_ngay.Text = dt_Xquang_CT.Rows[dong].Cells["ngay"].Value.ToString();
            txb_dongia.Text = dt_Xquang_CT.Rows[dong].Cells["dongia"].Value.ToString();
           
        }

        private void bt_chon_Click(object sender, EventArgs e)
        {
            string _mabn = frmkhambenh._ma;
            string _chondv = "";
            int n = ckListHuyetdo.CheckedItems.Count;
            for (int i = 0; i < n; i++)
            {
                _chondv += "N'" + ckListHuyetdo.CheckedItems[i] + "'";
                if (i < n - 1)
                    _chondv += ", ";

            }

            SqlConnection ketnoi = getconnect();
            ketnoi.Open();
            SqlCommand cmd = new SqlCommand(@"  SELECT * INTO temp FROM LOAI_DICH_VU_CLS WHERE TEN_LOAI_DVCLS IN (" + _chondv + ")", ketnoi);

            // var TB = new DataTable();
            _dtXQ.Load(cmd.ExecuteReader());

            _sum = Convert.ToInt64(_dtXQ.Compute("sum(don_gia)", ""));


            frmkhambenh frm = new frmkhambenh();
            frm._dtbKB = _dtXQ;
            frm._sum = _sum;
            this.Close();
        }

        private void dt_ngay_Click(object sender, EventArgs e)
        {

        }



    }
}
